/* SimpleApp.java */
import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FilterFunction;
import org.apache.spark.rdd.RDD;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.Dataset;
import scala.Tuple2;

import java.util.Arrays;

public class spark_java {
    public static void main(String[] args) {
        String logFile = "C:\\Users\\KC\\Desktop\\即饮咖啡.txt"; // Should be some file on your system

        SparkSession spark = SparkSession.builder().appName("Simple Application").master("local").getOrCreate();
        Dataset<String> logData = spark.read().textFile(logFile).cache();

        long numAs = logData.filter((FilterFunction<String>) s -> s.contains("a")).count();
        long numBs = logData.filter((FilterFunction<String>) s -> s.contains("b")).count();

        System.out.println("Lines with a: " + numAs + ", lines with b: " + numBs);

        SparkContext sc = spark.sparkContext();
        //读取数据
        var jrdd = sc.textFile("C:\\Users\\KC\\Desktop\\即饮咖啡.txt",2);
        //切割压平

//
////        var jrdd2 = jrdd.flatMap(line -> Arrays.asList(line.split(",")).iterator());
//        //和1组合
//        var jrdd2 = jrdd.flatMap(line -> line.split(",",2));
//
//        JavaPairRDD<String, Integer> jprdd = jrdd2.mapToPair(t -> new Tuple2<String, Integer>(t, 1));
//        //分组聚合
//        JavaPairRDD<String, Integer> res = jprdd.reduceByKey((a, b) -> a + b);
//        //保存
//        res.saveAsTextFile(args[1]);
//        //释放资源
//        jsc.close();



        spark.stop();







    }
}